/*
 * $Id: ActRequestDAO.java,v 1.14 2010/05/19 17:24:05 navdeep_mahajan Exp $
 * 
 * Copyright (C) 2006 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.common.database.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import com.ge.healthcare.autosc.common.ASCUtil;
import com.ge.healthcare.autosc.common.database.pojo.ActRequest;
import com.ge.healthcare.autosc.common.database.pojo.ActTask;
import com.ge.healthcare.autosc.common.database.pojo.CfgCodeDetails;
import com.ge.healthcare.autosc.common.exceptions.FaultRecorderException;

/**
 * @author 212042764
 * 
 */
public class ActRequestDAO extends BaseDAO implements IRequestDAO {

	/**
	 * Eclispe generated
	 */
	private static final long serialVersionUID = 6383937724058764423L;
	private ICodeDetailsDAO codeDetailsDAO;

	public ActRequestDAO() {
		super(ActRequest.TABLE_NAME);
	}

	public ActRequest getByRequestID(Long requestId) {
		Object request = findObjectByID(ActRequest.COL_REQUEST_ID, requestId);
		if (!ASCUtil.isNullOrEmptyObject(request)) {
			return (ActRequest) request;
		}
		return null;
	}

	public ActRequest getRequest(ActTask task) {
		return getByRequestID(task.getRequestId());
	}

	public ActRequest getRequest(ActRequest request) {
		if (request.getRequestId() == null) {
			throw new FaultRecorderException(
					"Request Object did not contain request id");
		}
		return getByRequestID(request.getRequestId());
	}

	public List<ActRequest> getRequestsbyRequestListId(Long requestListId) {
		List<?> rtvdRequests = findByProperty(ActRequest.COL_REQUEST_LIST_ID,
				requestListId);
		if (ASCUtil.isNullOrEmptyList(rtvdRequests)) {
			return null;
		}
		List<ActRequest> requestLst = new ArrayList<ActRequest>();
		for (Object request : rtvdRequests) {
			requestLst.add((ActRequest) request);
		}
		return requestLst;
	}

	public List<ActRequest> getRequestBySystemID(String systemId) {
		List<ActRequest> actRequestLst = new Vector<ActRequest>();
		List<?> requests = findByProperty(ActRequest.COL_SYSTEM_ID, systemId);
		if (!ASCUtil.isNullOrEmptyList(requests)) {
			for (int iCount = 0; iCount < requests.size(); iCount++) {
				actRequestLst.add((ActRequest) requests.get(iCount));
			}
		}
		return actRequestLst;
	}
	
	public List<ActRequest> getRequestByModelType(String modelType){
		List<ActRequest> actRequestLst = new Vector<ActRequest>();
		List<?> requests = findByProperty(ActRequest.COL_MODEL_TYPE, modelType);
		if (!ASCUtil.isNullOrEmptyList(requests)) {
			for (int iCount = 0; iCount < requests.size(); iCount++) {
				actRequestLst.add((ActRequest) requests.get(iCount));
			}
		}
		return actRequestLst;
	}

	/**
	 * @see com.ge.healthcare.autosc.common.database.dao.BaseDAO#save(java.lang.Object)
	 */
	public void save(Object pojo) {
		super.save(pojo);
	}

	/**
	 * @see com.ge.healthcare.autosc.common.database.dao.IRequestTypeDAO#saveAlways(java.lang.Object)
	 */
	public void saveAlways(Object pojo) {
		super.save(pojo);
	}

	/**
	 * @see com.ge.healthcare.autosc.common.database.dao.BaseDAO#deleteByID(java.lang.Object)
	 */
	public void deleteByID(Object pojo) {
		super.deleteByID(pojo);
	}

	/**
	 * @see com.ge.healthcare.autosc.common.database.dao.BaseDAO#refresh(java.lang.Object)
	 */
	public void refresh(Object pojo) {
		super.refresh(pojo);
	}

	public void setCodeDetails(ActRequest request, String codeVal) {
		CfgCodeDetails code = codeDetailsDAO
				.getUniqueCodedetailsByCodeName(codeVal);
		request.setCodeId(code.getCodeId());
		this.save(request);
	}

	public void setCodeDetailsAlways(ActRequest request, String codeVal) {
		CfgCodeDetails code = codeDetailsDAO
				.getUniqueCodedetailsByCodeName(codeVal);
		request.setCodeId(code.getCodeId());
		this.saveAlways(request);
	}

	/**
	 * @param codeDetailsDAO
	 *            the codeDetailsDAO to set
	 */
	public void setCodeDetailsDAO(ICodeDetailsDAO codeDetailsDAO) {
		this.codeDetailsDAO = codeDetailsDAO;
	}
	public List<ActRequest> getRequestByModality(String modality) {
		List<ActRequest> actRequestLst = new Vector<ActRequest>();
		List<?> requests = findByProperty(ActRequest.COL_MODALITY, modality);
		if (!ASCUtil.isNullOrEmptyList(requests)) {
			for (int iCount = 0; iCount < requests.size(); iCount++) {
				actRequestLst.add((ActRequest) requests.get(iCount));
			}
		}
		return actRequestLst;
	}
}
